package com.purchase.order.service.Impl;

import cn.com.flaginfo.sdk.cmc.api.request.ApiConfig;
import cn.com.flaginfo.sdk.cmc.api.result.ComResult;
import cn.com.flaginfo.sdk.cmc.api.sms.dynsend.DynSMSSendDataResult;
import cn.com.flaginfo.sdk.cmc.api.sms.dynsend.DynSMSSendRequest;
import com.purchase.common.api.SDKApi;
import com.purchase.common.config.PurchaseConfig;
import com.purchase.common.exception.file.InvalidExtensionException;
import com.purchase.common.utils.SecurityUtils;
import com.purchase.common.utils.file.FileUploadUtils;
import com.purchase.common.utils.file.MimeTypeUtils;
import com.purchase.common.utils.uuid.IdUtils;
import com.purchase.manage.damian.DAO.ProjectQuotationDAO;
import com.purchase.manage.damian.DAO.QuotationDAO;
import com.purchase.manage.damian.DAO.StoreQuotationDAO;
import com.purchase.manage.damian.*;
import com.purchase.manage.damian.VO.OrderAllQuotationVO;
import com.purchase.manage.damian.VO.OrderQuotationVO;
import com.purchase.manage.damian.VO.OrderVO;
import com.purchase.manage.mapper.ManageFileMapper;
import com.purchase.manage.mapper.ManagePurchaseMapper;
import com.purchase.manage.mapper.ManageSupplierMapper;
import com.purchase.manage.service.impl.IFileServiceImpl;
import com.purchase.order.domain.DAO.OrderDAO;
import com.purchase.order.mapper.EvaluateMapper;
import com.purchase.order.mapper.OrderMapper;
import com.purchase.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.testng.Assert;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static com.purchase.common.utils.PageUtils.startPage;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ManagePurchaseMapper managePurchaseMapper;

    @Autowired
    ManageFileMapper manageFile;

    @Autowired
    ManageSupplierMapper manageSupplierMapper;

    @Autowired
    IFileServiceImpl iFileService;

    @Autowired
    EvaluateMapper evaluateMapper;


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //采购员查看发布的订单
    @Override
    public List<OrderDAO> selectreleaseList(String type, Long orderId, String name) {
        return orderMapper.selectreleaseList(type, "3", orderId, name);
    }

    //查看工程类订单详细物资
    @Override
    public List<OrderProjectList> selectReleaseMaterialsListProject(Long orderId) {
        return orderMapper.selectReleaseMaterialsListProject(orderId);
    }

    @Override
    public List<StoreQuotationList> selectReleaseMaterialsListOther(Long orderId) {
        return orderMapper.selectReleaseMaterialsListOther(orderId);
    }

    //删除订单
    @Override
    @Transactional
    public void deleteOrder(Long orderId) {
        if (orderMapper.deleteOrder(orderId) != 1) {
            throw new SecurityException("删除订单失败");
        }
    }

    //供应商查看发布的订单
    @Override
    public List<OrderList> bidList(String type) throws ParseException {
        List<OrderList> orderDAOList = new ArrayList<>();
        Long supplierId = manageSupplierMapper.selectSupplierId(SecurityUtils.getUserId());
        Supplier supplier = manageSupplierMapper.selectSupplierById(supplierId);
        if (supplier==null || supplier.getTypeIds() == null) {
            return orderDAOList;
        }

        String[] ll = supplier.getTypeIds().split(",");
        int x = 0;
        for (int i = 0; i < ll.length; i++) {
            String xx = ll[i];
            List<OrderList> orderDAOS = orderMapper.selectList(type, xx);
            for (int ii = 0; ii < orderDAOS.size(); ii++) {
                if (orderDAOS != null) {
                    if (orderDAOList.size() == 0) {
                        Purchase purchase = managePurchaseMapper.selectPurchase(String.valueOf(orderDAOS.get(ii).getPurchaseId()));
                        orderDAOS.get(ii).setPurchaseName(purchase.getName());
                        orderDAOS.get(ii).setPurchasePhone(purchase.getPhone());
                        orderDAOList.add(orderDAOS.get(ii));
                    }
                    x = 0;
                    for (int j = 0; j < orderDAOList.size(); j++) {
                        if (orderDAOList.get(j).getOrderId().equals(orderDAOS.get(ii).getOrderId())) {
                            x = 1;
                        }
                    }
                    if (x == 0) {
                        Purchase purchase = managePurchaseMapper.selectPurchase(String.valueOf(orderDAOS.get(ii).getPurchaseId()));
                        orderDAOS.get(ii).setPurchaseName(purchase.getName());
                        orderDAOS.get(ii).setPurchasePhone(purchase.getPhone());
                        orderDAOList.add(orderDAOS.get(ii));
                    }
                }
            }
        }
        for (int i =0;i<orderDAOList.size();i++){
            int check = orderMapper.checkState(orderDAOList.get(i).getOrderId(), supplierId);
            if (check ==1){
                orderDAOList.remove(i);
                i--;
            }
        }
        for (int i =0;i<orderDAOList.size();i++){
            Date d1 = new Date();
            String xx = sdf.format(d1);
            Date startTime = sdf.parse(orderDAOList.get(i).getStartTime());
            String xx1 = sdf.format(startTime);
            if (startTime.after(d1)){
                orderDAOList.remove(i);
                i--;
            }
        }


        return orderDAOList;
    }

    //供应商删除上传的询价单
    @Override
    @Transactional
    public void deleteQutation(Long orderId, String type) throws ServerException {
        Long sid = manageSupplierMapper.selectSupplierId(SecurityUtils.getUserId());

        if ("工程类".equals(type)) {
            OrderProject project = orderMapper.selectQutataion(orderId, sid);
            if (project == null) {
                throw new ServerException("该订单不存在");
            }
            orderMapper.deleteQutationProject(project.getProjectId());
            orderMapper.deleteQutationProjectList(project.getProjectId());
            manageFile.updateSys(orderId, project.getSupplierId(), "0");
        } else {
            StoreQuotation project = orderMapper.selectQutataionOther(orderId, sid);
            if (project == null) {
                throw new ServerException("该订单不存在");
            }
            orderMapper.deleteQutationOther(project.getStoreQuotationId());
            orderMapper.deleteQutationOtherList(project.getStoreQuotationId());
            manageFile.updateSys(orderId, project.getSupplierId(), "0");
        }
    }

    //招标结束更改订单状态
    @Override
    @Transactional
    public void changeState() {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Long> list = orderMapper.selectChangeStateList(ft.format(date));
        if (list != null) {
            orderMapper.updateState(list);
        }
    }

    //查看招标详情 中 工程类询价单
    @Override
    public List<OrderProject> getQutationProjectInfo(OrderList orderDAO) {
        List<OrderProject> list = new ArrayList<>();
        if ("0".equals(orderDAO.getState())) {
            list = orderMapper.getQutationProjectInfo(orderDAO.getOrderId());
        } else {
            list = orderMapper.getQutationProjectInfoAll(orderDAO.getOrderId());
            if (list != null) {
                for (OrderProject o1 : list) {
                    List<OrderProjectList> lists = orderMapper.getQutationProjectListInfoAll(o1.getProjectId());
                    o1.setOrderProjectLists(lists);
                }
            }
        }
        return list;
    }

    @Override
    public List<StoreQuotation> getQutationOther(OrderList orderList) throws ParseException {
        List<StoreQuotation> list = new ArrayList<>();
        if ("0".equals(orderList.getState())) {
            list = orderMapper.getStoreQutationInfo(orderList.getOrderId());
        } else {
            List<StoreQuotationList> storeQuotationListList = historyListParity(orderList.getOrderId());
            list = orderMapper.getStoreQutationInfoAll(orderList.getOrderId());
            if (list != null) {
                for (StoreQuotation s1 : list) {
                    s1.setDtime(sdf.format(s1.getDeliveryTime()));
                    s1.setWtime(sdf.format(s1.getWarrantyTime()));
                    s1.setQtime(sdf.format(s1.getQuotationTime()));
                    List<StoreQuotationList> lists = xxxx(s1.getStoreQuotationId(), storeQuotationListList);
                    s1.setStoreQuotationLists(lists);
                }
            }
        }
        return list;
    }

    //对比价格 查找同名字的商品
    private List<StoreQuotationList> xxxx(Long stid, List<StoreQuotationList> storeQuotationListList) {
        List<StoreQuotationList> lists = orderMapper.getStoreQutationListInfoAll(stid);

        for (int i = 0; i < lists.size(); i++) {
            for (int j = 0; j < storeQuotationListList.size(); j++) {
                if (storeQuotationListList.get(j).getProduceName().equals(lists.get(i).getProduceName())) {
                    lists.get(i).setLowPrice(String.valueOf(storeQuotationListList.get(j).getUnitPrice()));
                }
            }
        }
        return lists;
    }

    //中标
    @Override
    @Transactional
    public void successBid(Long orderId, Long supplierId) throws ServerException {
        OrderList orderList = orderMapper.selectOrder(orderId);
        if ("2".equals(orderList.getState())) {
            throw new ServerException("该订单已中标");
        }
        if ("工程类".equals(orderList.getMaterialTypeName())) {
            List<OrderProject> list = orderMapper.getQutationProjectInfoAll(orderId);
            orderList.setSupplierId(supplierId);
            orderList.setWinningPrice(list.get(0).getCountPrice());
            orderList.setQuotationId(list.get(0).getProjectId());
            Supplier supplier = manageSupplierMapper.selectSupplierById(Long.valueOf(supplierId));
            String date =supplier.getHandledByPhone()  +","+ supplier.getHandledBy()+"," +supplier.getCorporateName();
            testDynSendMethod("手机号码,name,order",date,"2261012056599");
        } else {
            List<StoreQuotation> list = orderMapper.getStoreQutationInfoAll(orderId);
            orderList.setSupplierId(supplierId);
            orderList.setWinningPrice(list.get(0).getCountPrice());
            orderList.setQuotationId(list.get(0).getStoreQuotationId());
            Supplier supplier = manageSupplierMapper.selectSupplierById(Long.valueOf(supplierId));
            String date =supplier.getHandledByPhone()  +","+ supplier.getHandledBy()+"," +supplier.getCorporateName();
            testDynSendMethod("手机号码,name,order",date,"2261012056599");
        }
        //给未中标的 发短信
        loseBid(orderList,supplierId);
        orderMapper.updateSuccessBid(orderList);
    }

    //给未中标的 发短信
    private void loseBid(OrderList orderList, Long supplierId) {
        List<SysSupplierState> list = new ArrayList<>();
        Long orderId = orderList.getOrderId();
        String[] ll = orderMapper.getOrderTypids(orderId).split(",");
        int x = 0;
        for (int i = 0; i < ll.length; i++) {
            String xx = ll[i];
            List<Supplier> suList = orderMapper.getSupplierTypeIds(xx);
            for (int ii = 0; ii < suList.size(); ii++) {
                SysSupplierState sys = new SysSupplierState();
                if (list.size() == 0) {
                    int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                    if (check == 1) {
                        sys.setState("1");
                        sys.setOrderId(orderId);
                        sys.setSupplierId(suList.get(ii).getSupplierId());
                        sys.setCorporateName(suList.get(ii).getCorporateName());
                        list.add(sys);
                    }
                }
                x = 0;
                for (int j = 0; j < list.size(); j++) {
                    if (suList.get(ii).getSupplierId().equals(list.get(j).getSupplierId())) {
                        x = 1;
                    }
                }
                if (x == 0) {
                    int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                    if (check == 1) {
                        sys.setState("1");
                        sys.setOrderId(orderId);
                        sys.setSupplierId(suList.get(ii).getSupplierId());
                        sys.setCorporateName(suList.get(ii).getCorporateName());
                        list.add(sys);
                    }
                }
            }
            for (int j=0;i< list.size();i++){
                if (!list.get(j).getSupplierId().equals(supplierId)){
                    Supplier supplier = manageSupplierMapper.selectSupplierById(list.get(j).getSupplierId());
                    String date =supplier.getHandledByPhone()  +","+ supplier.getHandledBy()+"," +supplier.getCorporateName();
                    testDynSendMethod("手机号码,name,order",date,"2261012056607");
                }
            }
        }
    }

    //查看所有订单
    @Override
    public List<OrderDAO> selectList(String type, Long orderId, String materialTypeName, String state) {
        if (materialTypeName != null ){
            materialTypeName = materialTypeName.trim();
            if ( "undefined".equals(materialTypeName)){
                materialTypeName = null;
            }
        }
        Long userid = managePurchaseMapper.selectPurchaseByuid(SecurityUtils.getUserId());
        Long id = SecurityUtils.getUserId();
        if (id == 1 || id == 100) {
            startPage();
            return orderMapper.selectAll(type, orderId, materialTypeName, state, userid);
        }
        startPage();
        return orderMapper.selectAllList(type, orderId, materialTypeName, state, userid);
    }

    @Override
    public List<QuotationDAO> supplierQuotationListProject(Long orderId, String name) {
        return null;
    }

    //供应商查看所有商品询价单
    @Override
    public List<QuotationDAO> supplierQuotationListOther(Long orderId, String name) {
        List<QuotationDAO> list = new ArrayList<>();
//        List<StoreQuotation> list1  =orderMapper.supplierQuotationListOther(orderId,name,SecurityUtils.getUserId());
//        for (StoreQuotation o1:list1) {
//            QuotationDAO q1 =new QuotationDAO();
//            Boolean check =false;
//            String zxc =o1.getBid();
//            String vcx = String.valueOf(SecurityUtils.getUserId());
//            if (vcx.equals(zxc)){
//                check =true;
//            }
//            if ("0".equals(o1.getState())){
//                q1.setQuoteState("供应商报价中");
//            } else if ("1".equals(o1.getState())) {
//                q1.setQuoteState("供应商报价完成");
//            }else if( "2".equals(o1.getState()) && check == true ){
//                q1.setQuoteState("比价结束送货中 ");
//            }else {
//                q1.setQuoteState("订单结束");
//            }
//            q1.setStoreQuotation(o1);
//            list.add(q1);
//        }
        return list;
    }

    //历史比价
    @Override
    public List<StoreQuotation> priceComparison(String name) {
        if (name != null ){
             name = name.trim();
            if ( "undefined".equals(name)){
                name = null;
            }
        }

        return orderMapper.priceComparison(name);
    }

    //添加商品类订单
    @Override
    @Transactional
    public void addStoreOrder(OrderVO orderVO) throws ServerException, ParseException {
        Long orderId = IdUtils.randomSnowflake();
        OrderList orderList = new OrderList();
        Long purid = managePurchaseMapper.selectPurchaseByuid(SecurityUtils.getUserId());
        Purchase purchase = managePurchaseMapper.selectPurchase(String.valueOf(purid));
        if (purchase == null) {
            throw new ServerException("该采购员不存在");
        }
        orderList.setOrderName(orderVO.getOrderName());
        orderList.setOrderId(orderId);
        orderList.setPurchaseName(purchase.getName());
        orderList.setPurchaseId(purid);
        orderList.setMaterialTypeName(orderVO.getType());
        orderList.setStartTimeType(sdf.parse(orderVO.getStartTime()));
        orderList.setEndTimeType(sdf.parse(orderVO.getEndTime()));
        if (manageFile.insertOrder(orderList) != 1) {
            throw new ServerException("创建新订单失败，请重新填写表格");
        }

        //创建采购物资清单订单
        List<StoreQuotationList> list = orderVO.getStoreQuotationLists();
        for (StoreQuotationList s1 : list) {
            s1.setStoreId(IdUtils.randomSnowflake());
            s1.setOrderId(orderId);
            if (manageFile.insertQuotationList(s1) != 1) {
                throw new ServerException("创建新订单失败，请重新填写表格");
            }
        }
        System.out.println(orderVO);
    }

    //添加工程类订单
    @Override
    @Transactional
    public void addProjectOrder(OrderVO orderVO) throws ServerException, ParseException {
        Long orderId = IdUtils.randomSnowflake();
        Long purid = managePurchaseMapper.selectPurchaseByuid(SecurityUtils.getUserId());
        Purchase purchase = managePurchaseMapper.selectPurchase(String.valueOf(purid));

        OrderList orderList = new OrderList();
        orderList.setOrderId(orderId);
        orderList.setOrderName(orderVO.getOrderName());
        orderList.setPurchaseName(purchase.getName());
        orderList.setPurchaseId(purid);
        orderList.setMaterialTypeName("工程类");
        orderList.setAbutmentPhone(Long.valueOf(purchase.getPhone()));
        orderList.setStartTimeType(sdf.parse(orderVO.getStartTime()));
        orderList.setEndTimeType(sdf.parse(orderVO.getEndTime()));
        if (manageFile.insertOrder(orderList) != 1) {
            throw new ServerException("创建新订单失败，请重新填写表格");
        }

        //创建采购物资清单订单
        List<OrderProjectList> list = orderVO.getOrderProjectLists();
        for (OrderProjectList o1 : list) {
            o1.setProjectId(IdUtils.randomSnowflake());
            o1.setOrderId(orderId);
            if (manageFile.insertProjectQuotationList(o1) != 1) {
                throw new ServerException("创建新订单失败，请重新填写表格");
            }
        }
    }

    //供应商手动添加工程类订单
    @Override
    @Transactional
    public OrderProject insertSupplierAddProject(OrderQuotationVO orderQuotationVO) throws IOException, InvalidExtensionException {
        if (orderQuotationVO.getPhoto() == null) {
            throw new ServerException("盖章文件不能为空！");
        }
        OrderProject orderProject = new OrderProject();
        Long projectId = IdUtils.randomSnowflake();
        Long supplier = manageSupplierMapper.selectSupplierId(SecurityUtils.getUserId());
        orderProject.setOrderId(orderQuotationVO.getOrderId());
        orderProject.setProjectId(projectId);
        orderProject.setMaintenanceItems(orderQuotationVO.getMaintenanceItems());
        orderProject.setRepairDate(orderQuotationVO.getRepairDate());
        orderProject.setContacts(orderQuotationVO.getContacts());
        orderProject.setContactsPhone(String.valueOf(orderQuotationVO.getContactsPhone()));
        orderProject.setCorporateName(orderQuotationVO.getCorporateName());
        orderProject.setSupplierId(supplier);
        orderProject.setRepairLocation(orderQuotationVO.getRepairLocation());
        orderProject.setAbutmentPhone(Long.valueOf(orderQuotationVO.getAbutmentPhone()));
        orderProject.setCountPrice(orderQuotationVO.getCountPrice());
        orderProject.setQuotationCopyPhoto(FileUploadUtils.upload(PurchaseConfig.getAvatarPath(), orderQuotationVO.getPhoto(), MimeTypeUtils.IMAGE_EXTENSION));

        if (manageFile.insertOrderBySupplierProject(orderProject) != 1) {
            throw new ServerException("新增订单失败，请重新上传");
        }

        List<OrderProjectList> list = orderQuotationVO.getOrderProjectLists();
        for (OrderProjectList o1 : list) {
            o1.setProjectId(IdUtils.randomSnowflake());
            o1.setProjectQuotationId(projectId);
            if (manageFile.insertProjectQuotationList(o1) != 1) {
                throw new ServerException("新增订单失败，请重新上传");
            }
        }
        orderProject.setOrderProjectLists(list);
        return orderProject;
    }

    //供应商手动添加商品类订单
    @Override
    @Transactional
    public StoreQuotation insertSupplierAddstore(OrderQuotationVO orderQuotationVO) throws IOException, InvalidExtensionException {
        StoreQuotation store = new StoreQuotation();
        if (orderQuotationVO.getPhoto() == null) {
            throw new ServerException("盖章文件不能为空！");
        }
        Long storeQuotationId = IdUtils.randomSnowflake();
        Long supplier = manageSupplierMapper.selectSupplierId(SecurityUtils.getUserId());
        store.setOrderId(orderQuotationVO.getOrderId());
        store.setStoreQuotationId(storeQuotationId);
        store.setAddress(orderQuotationVO.getAddress());
        store.setEmail(orderQuotationVO.getEmail());
        store.setContacts(orderQuotationVO.getContacts());
        store.setContactsPhone(String.valueOf(orderQuotationVO.getContactsPhone()));
        store.setCorporateName(orderQuotationVO.getCorporateName());
        store.setSupplierId(supplier);
        store.setDeliveryTime(orderQuotationVO.getDeliveryTime());
        store.setWarrantyTime(orderQuotationVO.getWarrantyTime());
        store.setQuotationTime(orderQuotationVO.getQuotationTime());
        store.setType(orderQuotationVO.getType());
        store.setRemake(orderQuotationVO.getRemake());
        store.setCountPrice(orderQuotationVO.getCountPrice());
        store.setQuotationCopyPhoto(FileUploadUtils.upload(PurchaseConfig.getAvatarPath(), orderQuotationVO.getPhoto(), MimeTypeUtils.IMAGE_EXTENSION));
        if (manageFile.insertOrderBySupplier(store) != 1) {
            throw new ServerException("上传订单失败，请重新上传");
        }  //添加进订单从表

        List<StoreQuotationList> list = orderQuotationVO.getStoreQuotationLists();
        for (StoreQuotationList s1 : list) {
            s1.setStoreId(IdUtils.randomSnowflake());
            s1.setStoreQuotationId(storeQuotationId);
            if (manageFile.insertQuotationListBySupplier(s1) != 1) {
                throw new ServerException("上传订单失败，请重新上传");
            }
        }
        store.setStoreQuotationLists(list);
        return store;
    }

    //供应商查看所有工程询价单
    @Override
    public List<OrderAllQuotationVO> allQuotation(String name, Long orderId) {
        if (name != null ){
            name = name.trim();
            if ( "undefined".equals(name)){
                name = null;
            }
        }
        List<OrderAllQuotationVO> list = new ArrayList<>();
        Long supplier = manageSupplierMapper.selectSupplierId(SecurityUtils.getUserId());
        List<StoreQuotationDAO> storeList = orderMapper.supplierQuotationListOther(orderId, name, supplier);
        for (int i = 0; i < storeList.size(); i++) {
            OrderAllQuotationVO order = new OrderAllQuotationVO();
            order.setOrderId(String.valueOf(storeList.get(i).getOrderId()));
            order.setType("商品类");
            order.setOrderName(storeList.get(i).getOrderName());
            order.setPurchaseName(storeList.get(i).getPurchaseName());
            order.setCountPrice(storeList.get(i).getCountPrice());
            order.setEndTime(sdf.format(storeList.get(i).getEndTime()));
            order.setPhoto(storeList.get(i).getQuotationCopyPhoto());
            order.setState(storeList.get(i).getState());
            list.add(order);
        }
        List<ProjectQuotationDAO> projectList = orderMapper.supplierQuotationListProject(orderId, name, supplier);
        for (int i = 0; i < projectList.size(); i++) {
            OrderAllQuotationVO order = new OrderAllQuotationVO();
            order.setOrderId(String.valueOf(projectList.get(i).getOrderId()));
            order.setType("工程类");
            order.setOrderName(projectList.get(i).getOrderName());
            order.setPurchaseName(projectList.get(i).getPurchaseName());
            order.setCountPrice(projectList.get(i).getCountPrice());
            order.setEndTime(sdf.format(projectList.get(i).getEndTime()));
            order.setPhoto(projectList.get(i).getQuotationCopyPhoto());
            order.setState(projectList.get(i).getState());
            list.add(order);
        }
        return list;
    }

    //采购员确认收到货
    @Override
    public void deliveryOfGoods(Long orderId) throws ServerException {
        OrderList orderList = orderMapper.selectOrder(orderId);
        if (!"2".equals(orderList.getState())) {
            throw new ServerException("该订单不是中标单，请重新选择");
        }
        if (orderMapper.updateGoodsState(orderId) != 1) {
            throw new ServerException("确认收货失败，请联系管理员");
        }
    }

    //采购员可查看某一订单未报价状态的供应商
    @Override
    public List<SysSupplierState> selectStateList(Long orderId, String state) throws ServerException {
        List<SysSupplierState> list = new ArrayList<>();

        String[] ll = orderMapper.getOrderTypids(orderId).split(",");
        int x = 0;
        for (int i = 0; i < ll.length; i++) {
            String xx = ll[i];
            List<Supplier> suList = orderMapper.getSupplierTypeIds(xx);
            for (int ii = 0; ii < suList.size(); ii++) {
                if (state == null) {
                    SysSupplierState sys = new SysSupplierState();
                    if (list.size() == 0) {
                        int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                        if (check == 1) {
                            sys.setState("1");
                        } else if (check == 0) {
                            sys.setState("0");
                        } else {
                            throw new ServerException(suList.get(ii).getCorporateName() + "该公司报价单异常，请联系管理员");
                        }
                        sys.setOrderId(orderId);
                        sys.setSupplierId(suList.get(ii).getSupplierId());
                        sys.setCorporateName(suList.get(ii).getCorporateName());
                        list.add(sys);
                    }
                    x = 0;
                    for (int j = 0; j < list.size(); j++) {
                        if (suList.get(ii).getSupplierId().equals(list.get(j).getSupplierId())) {
                            x = 1;
                        }
                    }
                    if (x == 0) {
                        int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                        if (check == 1) {
                            sys.setState("1");
                        } else if (check == 0) {
                            sys.setState("0");
                        } else {
                            throw new ServerException(suList.get(ii).getCorporateName() + "该公司报价单异常，请联系管理员");
                        }
                        sys.setOrderId(orderId);
                        sys.setSupplierId(suList.get(ii).getSupplierId());
                        sys.setCorporateName(suList.get(ii).getCorporateName());
                        list.add(sys);
                    }
                } else if ("0".equals(state)) {
                    SysSupplierState sys = new SysSupplierState();
                    if (list.size() == 0) {
                        int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                        if (check == 0) {
                            sys.setState("0");
                            sys.setOrderId(orderId);
                            sys.setSupplierId(suList.get(ii).getSupplierId());
                            sys.setCorporateName(suList.get(ii).getCorporateName());
                            list.add(sys);
                        }
                    }
                    x = 0;
                    for (int j = 0; j < list.size(); j++) {
                        if (suList.get(ii).getSupplierId().equals(list.get(j).getSupplierId())) {
                            x = 1;
                        }
                    }
                    if (x == 0) {
                        int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                        if (check == 0) {
                            sys.setState("0");
                            sys.setOrderId(orderId);
                            sys.setSupplierId(suList.get(ii).getSupplierId());
                            sys.setCorporateName(suList.get(ii).getCorporateName());
                            list.add(sys);
                        }
                    }
                } else if ("1".equals(state)) {
                    SysSupplierState sys = new SysSupplierState();
                    if (list.size() == 0) {
                        int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                        if (check == 1) {
                            sys.setState("1");
                            sys.setOrderId(orderId);
                            sys.setSupplierId(suList.get(ii).getSupplierId());
                            sys.setCorporateName(suList.get(ii).getCorporateName());
                            list.add(sys);
                        }
                    }
                    x = 0;
                    for (int j = 0; j < list.size(); j++) {
                        if (suList.get(ii).getSupplierId().equals(list.get(j).getSupplierId())) {
                            x = 1;
                        }
                    }
                    if (x == 0) {
                        int check = orderMapper.checkState(orderId, suList.get(ii).getSupplierId());
                        if (check == 1) {
                            sys.setState("1");
                            sys.setOrderId(orderId);
                            sys.setSupplierId(suList.get(ii).getSupplierId());
                            sys.setCorporateName(suList.get(ii).getCorporateName());
                            list.add(sys);
                        }
                    }
                }
            }
        }
        return list;
    }

    //导入预订单查询历史最低价
    @Override
    public List<StoreQuotationList> historyList(HttpServletResponse response, MultipartFile file) throws IOException {

        List<StoreQuotationList> list = new ArrayList<>();
        //创建采购物资清单订单
        String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (".xls".equals(fileSuffix)) {
            list = iFileService.purchaseRead(file);
        } else if (".xlsx".equals(fileSuffix)) {
            list = iFileService.purchaseReadXlsx(file);
        } else {
            throw new ServerException("上传的文件格式不对，请重新上传");
        }

        for (StoreQuotationList ss : list) {
            StoreQuotationList storeQuotationList = new StoreQuotationList();
            storeQuotationList = orderMapper.selectMinProduce(ss.getProduceName());
            if (storeQuotationList != null) {
                ss.setLowPrice(String.valueOf(storeQuotationList.getUnitPrice()));
            }
        }
        return list;
    }

    @Override
    public void test() {

    }

    //提醒供应商报价
    @Override
    public void remind(String supplierId) {
        Supplier supplier = manageSupplierMapper.selectSupplierById(Long.valueOf(supplierId));
        String date =supplier.getHandledByPhone()  +","+ supplier.getHandledBy()+"," +supplier.getCorporateName();
        testDynSendMethod("手机号码,name,order",date,"2261012056587");
    }

    @Override
    public String waibuUrl(Long orderId) throws ServerException {
        String type = "需求";
        if(evaluateMapper.selectCount(orderId,type) !=0){
            throw new ServerException("已填完第三方评价单");
        }
//        String url = "http://10.255.15.180:8020/msgIndex"+"?orderId="+orderId ;
        String url = "http://124.222.171.192:8025/msgIndex"+"?orderId="+orderId ;
        return url;
    }

    public void testDynSendMethod(String titles,String datas, String templateId) {
        //参数变量名
        String[] titleArr = titles.split(",");
        //参数变量值
        String[] rows = datas.split(";");
        String[][] dyns = new String[rows.length+1][titleArr.length];
        dyns[0] = titleArr;
        int rowNum = 1;
        for(String row:rows){
            String[] cols = row.split(",");
            dyns[rowNum++] = cols;
        }
        //请求参数
        DynSMSSendRequest sendRequest = new DynSMSSendRequest();
        //模板ID
        sendRequest.setTemplateId(templateId);
        sendRequest.setDynData(dyns);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Random rd = new Random();
        int seq = rd.nextInt(900000) + 100000;
        //流水号,状态回执报告中一致.如果不需要状态回执报告请不要设置值
        sendRequest.setSerialNumber(sdf.format(new Date()) + seq);
        SDKApi sdkApi = null;
        String spCode ="272939";
        String appkey = "yg_cgxt";
        String appSecret = "69261ed2657bacef24d031474b0441f8";

        ApiConfig config = new ApiConfig(spCode, appkey, appSecret);
        sdkApi = new SDKApi(config);
        ComResult<DynSMSSendDataResult> result = sdkApi.testDynSendMethod(sendRequest);
        Assert.assertTrue(result.isSucc(), "动态发送结果");
    }

    //历史价格
    public List<StoreQuotationList> historyListParity(Long orderId) {
        List<StoreQuotationList> list = orderMapper.selectReleaseMaterialsListOther1(orderId);
        List<StoreQuotationList> list1 = new ArrayList<>();
        for (StoreQuotationList ss : list) {
            StoreQuotationList storeQuotationList = new StoreQuotationList();
            storeQuotationList = orderMapper.selectMinProduce(ss.getProduceName());
            if (storeQuotationList != null) {
                list1.add(storeQuotationList);
            }
        }

        return list1;
    }


}



